/**
 * @Author luof
 * @Date 2024/4/28 11:00
 * @Description:
 */
import {createStore} from "vuex";
import {reqAdminInfo} from "../api/admin.js";

const store = createStore({
    state: {
        isCollapse: false, //是否可关闭
        tabs: [{path: "/admin/page", title: "首页"}], //标签tabs
        adminInfo: {}
    },
    mutations: {
        /**
         * 修改折叠面板
         * @param state
         */
        changeIsCollapse(state) {
            state.isCollapse = !state.isCollapse
        },
        /**
         * 添加tabs
         * @param state
         * @param msg
         * @returns {Promise<unknown>}
         */
        changePushTabs(state, msg) {
            return new Promise((resolve) => {
                try {
                    if (!state.tabs) {
                        state.tabs = []
                    }
                    const isPathExist = state.tabs.some((item) => item.path === msg.path)
                    if (!isPathExist) {
                        //路径不存在,则添加路径到tabs
                        state.tabs.push(msg)
                        localStorage.setItem("tabs", JSON.stringify(state.tabs))
                    }
                    resolve()
                } catch (error) {
                    console.log(error)
                }
            })
        },
        /**
         * 移除tabs
         * @param state
         * @param path
         */
        changeRemoveTabs(state, path) {
            state.tabs = state.tabs.filter((item) => item.path !== path)
            localStorage.setItem("tabs", JSON.stringify(state.tabs))
        },
        /**
         * 本地存储获取tabs
         * @param state
         * @param msg
         */
        changeTabsByLocalStorage(state, msg) {
            state.tabs = msg
        },
        changeAdminInfo(state, msg) {
            state.adminInfo = msg
        }
    },
    actions: {
        async handleReqGetAdminInfo(context) {
            try {
                const res = await reqAdminInfo()
                context.commit("changeAdminInfo", res.data.data)
            } catch (error) {
                console.log(error);
            }
        }
    },
    getters: {},
    modules: {}
})

export default store